/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.GerenciarTREF;

import Biblioteca.Library;
import Biblioteca.VerificaErros;
import ClassesListas.Profissionais;
import ClassesUnicas.Profissional;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 * FrameDefault
 *
 * Versao 1.0
 *
 * Data 16/02/2013
 *
 * @author Igor
 */
public class FrameLocalizaProfissional extends FramePattern implements InterfaceFrame {

    //Atributos
    private Profissional profissional;
    private Profissionais profissionais;

    /**
     * Creates new form FrameDefault
     *
     * @param profissional
     * @param dlg
     */
    public FrameLocalizaProfissional(Profissional profissional, JDialog dlg) {
        initComponents();

        Library.reagirEnter(buttonOk);
        Library.reagirEnter(buttonLocalizar);
        Library.reagirEnter(buttonLimpar);
        Library.reagirEnter(buttonFechar);

        Library.semCaracterEspecial(textNome);

        this.profissionais = null;
        super.dlg = dlg;
        this.profissional = profissional;

        //Impede a mudanca de posicao das colunas
        tabelaProfissionais.getTableHeader().setReorderingAllowed(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelTitulo = new javax.swing.JPanel();
        labelTitulo = new javax.swing.JLabel();
        labelCamposObrigatorios = new javax.swing.JLabel();
        panelBotoes = new javax.swing.JPanel();
        buttonLocalizar = new javax.swing.JButton();
        buttonLimpar = new javax.swing.JButton();
        buttonOk = new javax.swing.JButton();
        buttonFechar = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        abaPesquisa = new javax.swing.JPanel();
        panelPesquisa = new javax.swing.JPanel();
        panelResultadoPesquisa = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaProfissionais = new javax.swing.JTable();
        labelNome = new javax.swing.JLabel();
        textNome = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setTitle("Localizar Profissionais");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        panelTitulo.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        labelTitulo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        labelTitulo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        labelTitulo.setText("Tela utilizada para localizar/visualizar dados dos Profissionais");

        labelCamposObrigatorios.setText("Campos marcados com * são obrigatórios");

        javax.swing.GroupLayout panelTituloLayout = new javax.swing.GroupLayout(panelTitulo);
        panelTitulo.setLayout(panelTituloLayout);
        panelTituloLayout.setHorizontalGroup(
            panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTitulo)
                    .addComponent(labelCamposObrigatorios))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelTituloLayout.setVerticalGroup(
            panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(labelTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelCamposObrigatorios))
        );

        panelBotoes.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        buttonLocalizar.setText("Localizar");
        buttonLocalizar.setToolTipText("Clique para realizar a pesquisa dos profissionais");
        buttonLocalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLocalizarActionPerformed(evt);
            }
        });

        buttonLimpar.setText("Limpar");
        buttonLimpar.setToolTipText("Clique para limpar os dados da pesquisa");
        buttonLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLimparActionPerformed(evt);
            }
        });

        buttonOk.setText("OK");
        buttonOk.setToolTipText("Clique para editar os dados do profissional selecionado");
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        buttonFechar.setText("Fechar");
        buttonFechar.setToolTipText("Cancela a operação e fecha a janela");
        buttonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelBotoesLayout = new javax.swing.GroupLayout(panelBotoes);
        panelBotoes.setLayout(panelBotoesLayout);
        panelBotoesLayout.setHorizontalGroup(
            panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotoesLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buttonOk)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonLocalizar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonLimpar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonFechar)
                .addContainerGap())
        );

        panelBotoesLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonFechar, buttonLimpar, buttonLocalizar, buttonOk});

        panelBotoesLayout.setVerticalGroup(
            panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonLimpar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonLocalizar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonOk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonFechar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        panelPesquisa.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "   Filtros   ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12), java.awt.Color.black)); // NOI18N

        panelResultadoPesquisa.setBorder(javax.swing.BorderFactory.createTitledBorder("   Resultado da Pesquisa   "));

        tabelaProfissionais.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabelaProfissionais);

        javax.swing.GroupLayout panelResultadoPesquisaLayout = new javax.swing.GroupLayout(panelResultadoPesquisa);
        panelResultadoPesquisa.setLayout(panelResultadoPesquisaLayout);
        panelResultadoPesquisaLayout.setHorizontalGroup(
            panelResultadoPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
        );
        panelResultadoPesquisaLayout.setVerticalGroup(
            panelResultadoPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
        );

        labelNome.setText("Nome:");

        javax.swing.GroupLayout panelPesquisaLayout = new javax.swing.GroupLayout(panelPesquisa);
        panelPesquisa.setLayout(panelPesquisaLayout);
        panelPesquisaLayout.setHorizontalGroup(
            panelPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelResultadoPesquisa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(panelPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNome)
                    .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelPesquisaLayout.setVerticalGroup(
            panelPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPesquisaLayout.createSequentialGroup()
                .addComponent(labelNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(panelResultadoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout abaPesquisaLayout = new javax.swing.GroupLayout(abaPesquisa);
        abaPesquisa.setLayout(abaPesquisaLayout);
        abaPesquisaLayout.setHorizontalGroup(
            abaPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPesquisa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        abaPesquisaLayout.setVerticalGroup(
            abaPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelPesquisa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Pesquisa", abaPesquisa);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1)
                    .addComponent(panelTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelBotoes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:
        limparCampos();
        buttonOk.setEnabled(this.profissional != null);
        tabelaProfissionais.getColumnModel().getColumn(0).setPreferredWidth(50);
        tabelaProfissionais.getColumnModel().getColumn(1).setPreferredWidth(tabelaProfissionais.getWidth() - 50);
    }//GEN-LAST:event_formComponentShown

    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        // TODO add your handling code here:
        if (tabelaProfissionais.getSelectedRowCount() > 0) {
            //Pego o valor da coluna selecionada na tabela, converto para String, converto String para Long, dai faco a pesquisa da pessoa
            this.profissional.setIdProfissional(Long.parseLong((String) tabelaProfissionais.getValueAt(tabelaProfissionais.getSelectedRow(), 0)));
        }
        super.dlg.dispose();
    }//GEN-LAST:event_buttonOkActionPerformed

    private void buttonLocalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLocalizarActionPerformed
        // TODO add your handling code here:
        try {
            String nome = null;

            if (!textNome.getText().isEmpty()) {
                nome = textNome.getText();
            }

            if (this.profissionais == null) {
                this.profissionais = new Profissionais();
            }
            this.profissionais.setProfissionais(this.profissionais.getProfissionalDAO().buscaProfissionais(null, nome));
            this.profissionais.listarProfissionais(tabelaProfissionais);
            if (tabelaProfissionais.getRowCount() != 0) {
                tabelaProfissionais.setRowSelectionInterval(0, 0);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, VerificaErros.retornaMensagemErro(e), "Erro!", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_buttonLocalizarActionPerformed

    private void buttonLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLimparActionPerformed
        // TODO add your handling code here:
        limparCampos();
        jTabbedPane1.setSelectedIndex(0);
    }//GEN-LAST:event_buttonLimparActionPerformed

    private void buttonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonFecharActionPerformed
        // TODO add your handling code here:
        if (super.dlg == null) {
            super.fecharAplicacao();
        } else {
            super.dlg.dispose();
        }
    }//GEN-LAST:event_buttonFecharActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel abaPesquisa;
    private javax.swing.JButton buttonFechar;
    private javax.swing.JButton buttonLimpar;
    private javax.swing.JButton buttonLocalizar;
    private javax.swing.JButton buttonOk;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel labelCamposObrigatorios;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelTitulo;
    private javax.swing.JPanel panelBotoes;
    private javax.swing.JPanel panelPesquisa;
    private javax.swing.JPanel panelResultadoPesquisa;
    private javax.swing.JPanel panelTitulo;
    private javax.swing.JTable tabelaProfissionais;
    private javax.swing.JTextField textNome;
    // End of variables declaration//GEN-END:variables

    @Override
    public void limparCampos() {
        Library.limpaTextos(panelPesquisa);
        textNome.grabFocus();
        //Limpa a tabela
        DefaultTableModel dtm = (DefaultTableModel) tabelaProfissionais.getModel();
        while (dtm.getRowCount() != 0) {
            dtm.removeRow(0);
        }
    }

    @Override
    public void mostraDadosObjeto() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
